<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../common.inc.php';
require_once dirname(__FILE__) . '/Image.class.php';

$myPdo = DbUtils::createPdoInst();
$image = new Image();

$action = MiscUtils::getParam('action', NULL);
try {
  $myPdo->beginTransaction();
  switch ($action, $image) {
    case 'get':
      get($myPdo, $image);
      break;
    case 'save':
      save($myPdo, $image);
      break;
    case 'del':
      del($myPdo, $image);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}

function get($myPdo, $image) {
  
  $im = MiscUtils::getParam('im', 0);
  
  $notes = $image->data($im);
  echo json_encode($notes);
}

function save($myPdo) {
  $n = new stdClass();
  $n->id = ($_POST['id'] != 'undefined') ? $_POST['id'] : NULL;
  $n->track_id = $_POST['track_id'];
  $n->image_id = $_POST['image_id'];
  $n->top = $_POST['top'];
  $n->left = $_POST['left'];
  $n->width = $_POST['width'];
  $n->height = $_POST['height'];
  $n->text = $_POST['text'];
  $n->editable = $_POST['editable'];

  if ($n->id != NULL) {
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_CUSTOM . $image->folder . '_note', $n);
  } else {
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_CUSTOM . $image->folder . '_note', $n);
  }
  echo json_encode(array());
}

function del($myPdo) {
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = ?';
  $cond_vals->v = array($_POST['id']);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_CUSTOM . $image->folder . '_note', $cond_vals);
  echo json_encode(array());
}

?>